package com.itheima.admin.controller;

import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.PageDto;
import com.itheima.admin.dto.RoleUserDto;
import com.itheima.admin.dto.UserDto;
import com.itheima.admin.service.IUserService;
import com.itheima.admin.vo.UserPageVo;
import com.itheima.admin.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(value = "管理后台用户模块接口" , tags = "admin_user",description = "用于实现后台用户的增删改查操作")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询用户")
    public UserVo queryById(@PathVariable("id")  String id){
        return userService.queryById(id);
    }
    @DeleteMapping("/{id}")
    @ApiOperation("根据ID删除用户")
    public Result deleteById(@PathVariable("id")  String id){
        boolean result = userService.deleteById(id);
        return new Result(result,result?"删除用户成功":"删除用户失败",null);
    }
    @PostMapping("/list")
    @ApiOperation("分页列表查询用户")
    public PageVo<UserPageVo> queryByPage(@RequestBody PageDto pageDto){
        if(pageDto.getCurrentPage() == null){
            pageDto.setCurrentPage(1);
        }
        if(pageDto.getPageSize() == null){
            pageDto.setPageSize(10);
        }
        return userService.queryByPage(pageDto);
    }
    @PostMapping("/add")
    @ApiOperation("添加用户")
    public Result addUser(@RequestBody UserDto userDto){
        boolean result = userService.addUser(userDto);
        return new Result(result,result?"添加用户成功":"添加用户失败",null);
    }
    @PutMapping("/edit")
    @ApiOperation("修改用户")
    public Result editUser(@RequestBody UserDto userDto){
        boolean result = userService.editUser(userDto);
        return new Result(result,result?"修改用户成功":"修改用户失败",null);
    }
    @PostMapping("/role")
    @ApiOperation("用户授权")
    public Result roleUser(@RequestBody RoleUserDto roleUserDto){
        boolean result = userService.roleUser(roleUserDto);
        return new Result(result,result?"用户授权成功":"用户授权失败",null);
    }
}
